<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Shop\Controller;

use Admin\Data\Common;
use Doctrine\ORM\EntityManager;
use Goods\Entity\GoodsOtherTag;
use Goods\Entity\GoodsOtherTagGoods;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Mvc\I18n\Translator;
use Laminas\View\Model\ViewModel;

class OtherController extends AbstractActionController
{
    private $translator;
    private $entityManager;

    public function __construct(
        Translator      $translator,
        EntityManager   $entityManager
    )
    {
        $this->translator   = $translator;
        $this->entityManager= $entityManager;
    }

    public function indexAction()
    {
        return $this->redirect()->toRoute('shop');
    }

    /**
     * 聚合标签显示
     * @return array|\Laminas\Http\Response|ViewModel
     */
    public function tagGoodsAction()
    {
        $otherTagId = (int) $this->params()->fromRoute('id', -1);

        $otherTagInfo = $this->entityManager->getRepository(GoodsOtherTag::class)->findOneBy(['otherTagId' => $otherTagId, 'otherTagState' => 1]);
        if ($otherTagInfo == null) {
            return $this->redirect()->toRoute('shop');
        }

        $this->layout()->setVariables([
            'titleName'         => $otherTagInfo->getOtherTagTitle(),
            'extendKeywords'    => $otherTagInfo->getOtherTagKeywords(),
            'extendDescription' => $otherTagInfo->getOtherTagDescription()
        ]);

        $tagGoodsList = $this->entityManager->getRepository(GoodsOtherTagGoods::class)->shopFindOtherTagGoodsList($otherTagInfo->getOtherTagId(), $this->shopPlugin()->getUserLoginData('user_group_id'));

        if (!empty($otherTagInfo->getOtherTagTemplateFile()) && file_exists('module/Shop/view/'.Common::getShopDefaultTemplate().'/shop/other/'.basename($otherTagInfo->getOtherTagTemplateFile()).'.phtml')) {
            $view = new ViewModel();
            $view->setTemplate('shop/other/'.$otherTagInfo->getOtherTagTemplateFile());
            return $view->setVariables(['otherTagInfo' => $otherTagInfo, 'tagGoodsList' => $tagGoodsList]);
        }

        return ['otherTagInfo' => $otherTagInfo, 'tagGoodsList' => $tagGoodsList];
    }
}